//
//  DirData.m
//

#import "DirData.h"


@implementation DirData

//------------------------------------------------------------------------------
- (id) initWithPath: (NSString *) path   showDotFolders:(BOOL)pShowDotFolders
{
	self = [super init];
	if (nil != self)
	{
		showDotFolders = pShowDotFolders;
		fullPath = [path copy];
		name = [fullPath lastPathComponent];
		[name retain];
	}

	return self;
}

//------------------------------------------------------------------------------
- (id) initWithName: (NSString *)nodeName  showDotFolders:(BOOL)pShowDotFolders
{
	self = [super init];
	if (nil != self)
	{
		showDotFolders = pShowDotFolders;
		name = nodeName;
		[name retain];
	}

	return self;
}

//------------------------------------------------------------------------------
- (void) setFullPath: (NSString*) path
{
	[fullPath release];
	[name release];

	fullPath = path;
	[fullPath retain];
	name = [fullPath lastPathComponent];
	[name retain];
}

//------------------------------------------------------------------------------
- (void) sortChildren
{
	// Sort by name (case-insensitive)
	if (nil != childDirs)
	{
		[childDirs sortUsingSelector: @selector(compareDirData:)];
	}
}

//------------------------------------------------------------------------------
- (NSComparisonResult) compareDirData:(DirData*) dd
{
	return [name caseInsensitiveCompare: [dd name]];
}

//------------------------------------------------------------------------------
- (NSArray *) children
{
	if (nil == childDirs)
	{
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isDir, valid = [fileManager fileExistsAtPath: fullPath  isDirectory: &isDir];

        if (valid && isDir)
		{
            NSArray *array = [fileManager directoryContentsAtPath: fullPath];
            int cnt, numChildren = [array count];
            childDirs = [[NSMutableArray alloc] initWithCapacity: numChildren];
			for (cnt = 0; cnt < numChildren; ++cnt)
			{
				NSString *childName = [array objectAtIndex: cnt];

				NSString *childPath = [fullPath stringByAppendingPathComponent: childName];
				FSRef fsr;
				OSErr err = FSPathMakeRef((const UInt8*)[childPath fileSystemRepresentation], &fsr, NULL);
				if (noErr != err)
				{
					NSLog(@"FSPathMakeRef failed for %@, %d", childPath, err);
					continue;
				}

				Boolean isFolder;
				Boolean wasAliased;
				err = FSResolveAliasFile(&fsr, TRUE, &isFolder, &wasAliased);
				if (noErr != err)
				{
					NSLog(@"FSResolveAliasFile failed for %@, %d", childPath, err);
					continue;
				}

				if (TRUE == wasAliased)
				{
					UInt8 buffer[PATH_MAX];
					err = FSRefMakePath(&fsr, buffer, PATH_MAX);
					childPath = [NSString stringWithUTF8String: (char*)buffer];
				}

				if (isFolder)
				{
					if (NO == showDotFolders && [childName length] > 0 &&
						'.' == [childName characterAtIndex: 0])
					{
						continue;
					}

					DirData *newChild = [[DirData alloc] 
							initWithPath: childPath  
							showDotFolders:showDotFolders];

					[childDirs addObject: newChild];
					[newChild release];
				}
			}
		}
		else
		{
			childDirs = [[NSMutableArray alloc] initWithCapacity: 0];
		}
	}

	return childDirs;
}

//------------------------------------------------------------------------------
- (void) addChild:(DirData *) ddChild
{
	[self children];
	[childDirs addObject: ddChild];
}

//------------------------------------------------------------------------------
- (int) numberOfChildren
{
	return [[self children] count];
}

//------------------------------------------------------------------------------
- (DirData *) childAtIndex: (int) index
{
	return [[self children] objectAtIndex: index];
}

//------------------------------------------------------------------------------
- (NSString *) fullPath
{
	return fullPath;
}

//------------------------------------------------------------------------------
- (NSString *) name
{
	return name;
}

//------------------------------------------------------------------------------
- (void) dealloc
{
	[fullPath release];
	[name release];
	[childDirs release];
	[super dealloc];
}

@end
